package com.myapp.ruyixing.ui.shopmanager;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ActivityInfo;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

public class ActivityDetailActivity extends BaseActivity {
	private RequestQueue mQueue;
	private ImageLoader mImageLoader;

	private TitleBar mTitleBar;
	private View mLoadingView;

	private TextView mTitleView;
	private NetworkImageView mImageView;
	private TextView mPriceView;
	private TextView mTimeView;
	private TextView mParticipantView;
	private TextView mContentView;
	private TextView mContentTitleView;
	private ViewGroup mBody;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ActivityInfo info = (ActivityInfo) o;
				onLoadingDataFinished(true, info);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopbduss, String aid) {
		showLoading();
		RYHttpClient
				.getActivityInfo(this, mHttpResponseHandler, aid, shopbduss);
	}

	private void onLoadingDataFinished(boolean succeed, ActivityInfo info) {
		hideLoading();
		if (succeed && info != null) {
			loadViews(info);
		} else {
			Toast.makeText(this, "加载失败" + (info == null ? "" : info.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	private void loadViews(ActivityInfo info) {
		mBody.setVisibility(View.VISIBLE);
		mTimeView.setText("截止时间：" + info.datetime);
		mTitleView.setText(info.title);
		mParticipantView.setText("参与人数：" + info.num);
		final String aid = info.aid;
		mParticipantView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startParticipantManageActivity(aid);
			}
		});
		mImageView.setImageUrl(info.picurl, mImageLoader);
		String text = "原价：￥" + info.originalprice + "  现价：￥"
				+ info.presentprice;
		mPriceView.setText(text);
		addFontColor();

		if (info.items != null && info.items.size() > 0) {
			for (int i = 0; i < info.items.size(); i++) {
				ActivityInfo.InfoItem item = info.items.get(i);
				if (!TextUtils.isEmpty(item.content)
						&& !TextUtils.isEmpty(item.title)) {
					mContentView.setText(item.content);
					mContentTitleView.setText(item.title);
					mContentTitleView.setVisibility(View.VISIBLE);
					mContentView.setVisibility(View.VISIBLE);
				} else if (!TextUtils.isEmpty(item.picurl)) {
					addImages(item.picurl);
				}
			}
		}
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		mQueue = Volley.newRequestQueue(ActivityDetailActivity.this);
		mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());

		setContentView(R.layout.activity_shop_manager_activity_detail);

		initView();

		String aid = getIntent().getStringExtra("aid");
		onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(), aid);
	}

	private void startParticipantManageActivity(String aid) {
		startActivity(new Intent(this, ParticipantManageActivity.class)
				.putExtra("aid", aid));
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		mTitleView = (TextView) findViewById(R.id.title);
		mImageView = (NetworkImageView) findViewById(R.id.image);
		mTimeView = (TextView) findViewById(R.id.time);
		mPriceView = (TextView) findViewById(R.id.price);
		mParticipantView = (TextView) findViewById(R.id.participants);
		mContentView = (TextView) findViewById(R.id.content);
		mContentTitleView = (TextView) findViewById(R.id.content_title);
		mBody = (ViewGroup) findViewById(R.id.list);
	}

	private void addFontColor() {
		String text = mPriceView.getText().toString();
		Spannable wordtoSpan = new SpannableString(text);

		int assignStart = text.indexOf("现价：");
		wordtoSpan.setSpan(
				new ForegroundColorSpan(getResources().getColor(
						R.color.text_normal_price)), assignStart + 3,
				text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		mPriceView.setText(wordtoSpan);
	}

	private void addImages(String picurl) {
		View v = LayoutInflater.from(this).inflate(
				R.layout.view_activity_detail, mBody, false);
		((NetworkImageView) v.findViewById(R.id.image)).setImageUrl(picurl,
				mImageLoader);
		mBody.addView(v);
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}
}
